/**************游戏类**************/
function Game() {
	this.player;
	this.playerPlane;
	this.isStop;
	this.allScore;
	this.enemyArr;
	this.propArr;
	this.timer1; // 定时生成敌机
	this.timer2; //	定时生成道具
	this.runID;
	this.current = page1;
	this.bg;
	this.timer; // 游戏计时器
}

// 游戏初始化
Game.prototype.init = function() {
	this.allScore = 0;
	this.allTime = 60;
	this.enemyArr = [];
	this.propArr = [];
	this.current = page1;
	let childs = canvas.childNodes;
	score.innerHTML = this.allScore;
	for (let i = childs.length - 1; i >= 0; i--) {
		canvas.removeChild(childs[i]);
	}

	// 计时器
	this.timer = new Timer(game_timer);
}

// 游戏开始
Game.prototype.start = function() {
	// 初始化
	this.init();

	// 开始计时
	this.timer.start();

	// 显示画布
	page1.classList.remove('current');
	page3.classList.remove('current');
	page2.classList.add('current');
	this.current = page2;

	// 测量画布
	cHeight = canvas.offsetHeight;
	cWidth = canvas.offsetWidth;

	// 设置背景,速度
	this.bg = new Bg(bg, 5);

	// 出现玩家飞机
	this.playerPlane = new PlayerPlane('img/myplane.gif', cWidth / 2 - planeArr['player'].width / 2, 480, planeArr[
		'player'].speed);

	// 生成敌机
	this.createEnemy();

	// 生成道具
	this.createProps();

	// 控制敌机移动和删除，碰撞检测
	this.run();

	// 监听按键事件
	document.addEventListener('keydown', keyDown);
	document.addEventListener('keyup', keyUp);
}


// 造敌机
Game.prototype.createEnemy = function() {
	this.timer1 = setTimeout(() => {
		let degree = Math.floor(this.timer.s / 12);
		degree = degree < 6 ? degree : 5; // 最大难度为5
		// if (this.timer.s > 60) {
		// 	return;
		// }
		let num = rnd(0, 100);
		let type = num < degreeArr[degree][0] ? 'enemy1' : num < degreeArr[degree][1] ? 'enemy2' : 'enemy3';
		let enemy = new EnemyPlane(type, rnd(0, cWidth - planeArr[type].width), 0 - planeArr[type].height,
			planeArr[type].speed);
		this.enemyArr.push(enemy);
		this.createEnemy();
	}, 1000);
}


// 生成道具
Game.prototype.createProps = function() {
	this.timer2 = setTimeout(() => {
		let num = rnd(0, 100);
		let type = num < 70 ? 'ufo2' : num < 90 ? 'ufo1' : 'ufo3';
		let prop = new Prop(type);
		this.propArr.push(prop);
		this.createProps();
	}, rnd(20000, 30000));
}

// 以浏览器固定帧率执行操作
Game.prototype.run = function() {
	this.runID = requestAnimationFrame(() => {
		this.control(this.enemyArr);
		this.control(this.propArr);
		this.collision();
		this.bg.bgMove();
		this.playerPlane.controlBullet();
		this.playerPlane.action();
		this.run();
	});
}

// 控制物体移动
Game.prototype.control = function(arr) {
	for (let i = 0; i < arr.length; i++) {
		if (arr[i].move()) {
			arr.splice(i, 1);
		}
	}
}


// 碰撞检测监听函数
Game.prototype.collision = function() {

	// 子弹与敌机碰撞
	for (let i = 0; i < this.playerPlane.bulletArr.length; i++) {
		for (let j = 0; j < this.enemyArr.length; j++) {
			if (testCross(this.playerPlane.bulletArr[i], this.enemyArr[j])) {
				if (this.enemyArr[j].injured(this.playerPlane.harm)) { // 为true说明敌机死亡
					this.allScore += this.enemyArr[j].score;
					this.enemyArr.splice(j, 1);
					score.innerHTML = this.allScore;
				}
				canvas.removeChild(this.playerPlane.bulletArr[i].node);
				this.playerPlane.bulletArr.splice(i, 1);
				break;
			}
		}
	}

	// 道具与玩家飞机碰撞
	for (let i = 0; i < this.propArr.length; i++) {
		if (testCross(this.playerPlane, this.propArr[i])) {
			this.playerPlane.getProps(this.propArr[i].type);
			canvas.removeChild(this.propArr[i].node);
			this.propArr.splice(i, 1);
		}
	}

	// 玩家飞机与敌机碰撞
	// this.playerPlane.blood.setAttribute('data-percent',this.blood);
	for (let i = 0; i < this.enemyArr.length; i++) {
		if (testCross(this.playerPlane, this.enemyArr[i])) {
			if (this.playerPlane.injured(this.enemyArr[i].harm['collision'])) { // 为true说明玩家飞机死亡
				setTimeout(()=>this.end(), 1200);	// 游戏结束,等玩家死亡动画结束
			}
			canvas.removeChild(this.enemyArr[i].node);
			this.enemyArr.splice(i, 1);
			blood.setAttribute('data-percent', this.playerPlane.blood);
			blood.style.backgroundPositionX = blood_width * (planeArr[`${this.playerPlane.type}`].blood - this.playerPlane.blood) /
				planeArr[`${this.playerPlane.type}`].blood + 'px';
			break;
		}
	}
}

// 暂停游戏
Game.prototype.stop = function() {
	// 暂停计时
	this.timer.stop();

	page1.classList.remove('current');
	page3.classList.add('current');
	this.current = page3;
	this.isStop = true;

	let that = this;
	clearTimeout(that.timer1);
	clearTimeout(that.timer2);
	cancelAnimationFrame(that.runID);
}

// 继续游戏
Game.prototype.proceed = function() {
	if (this.isStop) {
		// 继续计时
		this.timer.start();

		page1.classList.remove('current');
		page3.classList.remove('current');
		this.current = page2;
		this.current.classList.add('current');

		this.createEnemy();
		this.createProps();
		this.run();
		return false;
	} else {
		return true;
	}
}

// 游戏结束
Game.prototype.end = function() {
	// 结束计时
	this.timer.reset();

	this.current = page4;
	this.current.classList.add('current');

	let that = this;
	document.removeEventListener('keydown', keyDown);
	document.removeEventListener('keyup', keyUp);
	clearTimeout(that.timer1);
	clearTimeout(that.timer2);
	cancelAnimationFrame(that.runID);
	this.init();
}

// 回到主页面
Game.prototype.backHome = function() {
	page2.classList.remove('current');
	page3.classList.remove('current');
	page4.classList.remove('current');
	this.current = page1;
	this.current.classList.add('current');
}


/**************游戏类**************/
